using System;
using System.IO;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace THANHTUNGO.Portal.Helpers
{
    /// <summary>
    /// thanhtungo.
    /// </summary>
    public class CrystalReport
    {
        private static void CopyData(Stream FromStream, Stream ToStream)
        {
            int intBytesRead;
            const int intSize = 4096;
            Byte[] buffer = new Byte[intSize];
            intBytesRead = FromStream.Read(buffer, 0, intSize);
            while (intBytesRead > 0)
            {
                ToStream.Write(buffer, 0, intBytesRead);
                intBytesRead = FromStream.Read(buffer, 0, intSize);
            }
        }

        public static void DisplayReportAsPdf(HttpResponse res, ReportDocument rpt)
        {
            MemoryStream ms = new MemoryStream();
            res.ClearContent();
            res.ClearHeaders();
            res.ContentType = "application/pdf";
            Stream str = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
            CopyData(str, ms);
            res.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
            ms.Close();
            res.Flush();
            res.Close();
        }

        public static void DisplayReportAsExcel(HttpResponse res, ReportDocument rpt)
        {
            MemoryStream ms = new MemoryStream();
            res.ClearContent();
            res.ClearHeaders();
            res.ContentType = "application/xls";
            Stream str = rpt.ExportToStream(ExportFormatType.Excel);
            CopyData(str, ms);
            res.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
            ms.Close();
            res.Flush();
            res.Close();
        }

    }
}
